package com.imooc.pan.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.pan.converter.OrdersConverter;
import com.imooc.pan.entity.StorageBusiness;
import com.imooc.pan.entity.StorageOrder;
import com.imooc.pan.mapper.StorageOrderMapper;
import com.imooc.pan.service.StorageBusinessService;
import com.imooc.pan.service.StorageOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author Janson
* @description 针对表【storage_order(存储购买订单表)】的数据库操作Service实现
* @createDate 2023-07-31 20:38:30
*/
@Service
@Slf4j
public class StorageOrderServiceImpl extends ServiceImpl<StorageOrderMapper, StorageOrder>
    implements StorageOrderService{
    
    @Autowired
    private StorageBusinessService storageBusinessService;
    
    @Autowired
    private OrdersConverter ordersConverter;
    
    @Override
    public String saveUserOrder(Long businessId) {
        StorageBusiness storageBusiness = storageBusinessService.getById(businessId);
        if (Objects.isNull(storageBusiness)){
            log.error("商品id：{} ，该商品不存在，请核查",businessId);
            throw new RuntimeException("当前商品不存在，请勿下单");
        }
        StorageOrder storageOrder = ordersConverter.assembleStorageOrderInfo(storageBusiness);
        // 创建订单时，设置订单状态，未支付。
        storageOrder.setStatus("0");
        storageOrder.setPayNo(String.valueOf(IdUtil.getSnowflakeNextId()));
        save(storageOrder);
        return storageOrder.getPayNo();
    }
}




